import React, {PureComponent} from "react"
import {bindActionCreators} from "redux"
import {connect} from "react-redux"
import {Form,Input,Button} from "antd"
import {updatePassword} from "../../../user/actions/index"

@connect(
    ({user: {userInfo}}) => ({
      userInfo
    }),
    (dispatch) => (
        bindActionCreators({
          updatePassword
        }, dispatch)
    )
)
class changePassword extends PureComponent {
  render() {
    let minError = {
      min: 6,
      message: "密码最少6个字符！"
    }
    const { getFieldDecorator } = this.props.form
    return (
      <Form
        layout="horizontal">
        <Form.Item
          label="新密码："
          labelCol={{ span: 2 }}
          wrapperCol={{ span: 10 }}
        >
          {getFieldDecorator("pwd", {
            rules: [{ required: true, message: "请输入新密码，至少6位字符！" }, minError],
            onChange: () => {},
          })(
            <Input
              type="password" placeholder="请输入新密码，至少6位字符！"/>
                    )}
        </Form.Item>
        <Form.Item
          label="确定密码："
          labelCol={{ span: 2 }}
          wrapperCol={{ span: 10 }}
        >
          {getFieldDecorator("pwd2", {
            rules: [{ required: true, message: "再次输入密码!" }, {validator: this.checkPassword}],
          })(
            <Input
              type="password" placeholder="再次输入密码!"/>
                    )}
        </Form.Item>
        <Form.Item>
          <Button
            type="primary" size="large" onClick={this.updatePwd}>确定</Button>
        </Form.Item>
      </Form>
    )
  }
  componentDidMount() {

  }
  updatePwd=() => {
    let {form, userInfo, updatePassword} = this.props
    form.validateFields((error, values) => {
      updatePassword({
        id: userInfo.id,
        oldPwd: values.oldPwd,
        pwd: values.pwd
      })
    })
  }
  checkPassword = (rule, value, callback) => {
    const form = this.props.form
    if (value && value !== form.getFieldValue("pwd")) {
      callback("二次输入密码相同!")
    } else {
      callback()
    }
  }
}
export default  Form.create()(changePassword)
